package com.ycode.bang.anbang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.baseui.BaseFragment;
import com.ycode.bang.anbang.fragment.BeingHandleFragment;
import com.ycode.bang.anbang.fragment.HadHandleFragment;
import com.ycode.bang.anbang.fragment.WaitHandleFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by yili on 2015/10/26.
 * Email : yili270@163.com
 * Description : 我的订单界面
 */
public class RepairListActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.btn_handle_wait)
    Button mBtnWait;
    @InjectView(R.id.btn_handle_doing)
    Button mBtnMiss;
    @InjectView(R.id.btn_handle_done)
    Button mBtnDelivery;
    public BaseFragment mWaitFragment;
    private BaseFragment mDoingFragment;
    private BaseFragment mDoneFragment;
    private List<BaseFragment> mFragmentList;
    private int mPosition;
    private receivePushMassage receivePushMassage = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addOurView(R.layout.activity_list_repair);
        ButterKnife.inject(this);
        initData(mBtnWait);
        initView();
        getBundle();
        registMassage();
    }

    private void getBundle() {
        String type = getIntent().getStringExtra("FjpushType");
        if(!TextUtils.isEmpty(type)){
        switch (type){
            case "ORDER_DELIVERED":
                mBtnDelivery.performClick();
                break;
            case "ORDER_WAIT_QUOTE":
                mBtnWait.performClick();
                break;
        }
        }
    }

    private void initData(Button button) {
        button.setSelected(true);

        mFragmentList = new ArrayList<>();
        mWaitFragment = new WaitHandleFragment();
        mDoingFragment = new BeingHandleFragment();
        mDoneFragment = new HadHandleFragment();
        mFragmentList.add(mWaitFragment);
        mFragmentList.add(mDoingFragment);
        mFragmentList.add(mDoneFragment);

    }

    private void initView() {
        titleTextView.setText("各类报修列表");
        mBtnWait.setOnClickListener(this);
        mBtnMiss.setOnClickListener(this);
        mBtnDelivery.setOnClickListener(this);
        getSupportFragmentManager().beginTransaction().add(R.id.fl_order, mWaitFragment)
                .add(R.id.fl_order, mDoingFragment).add(R.id.fl_order, mDoneFragment)
                .hide(mDoneFragment).hide(mDoingFragment)
                .show(mWaitFragment).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_handle_wait:
                if (!mBtnWait.isSelected()) {
                    mBtnWait.setSelected(true);
                    mBtnMiss.setSelected(false);
                    mBtnDelivery.setSelected(false);
                    switchFragment(0);
                }
                break;
            case R.id.btn_handle_doing:
                if (!mBtnMiss.isSelected()) {
                    mBtnWait.setSelected(false);
                    mBtnMiss.setSelected(true);
                    mBtnDelivery.setSelected(false);
                    switchFragment(1);
                }
                break;
            case R.id.btn_handle_done:
                if (!mBtnDelivery.isSelected()) {
                    mBtnWait.setSelected(false);
                    mBtnMiss.setSelected(false);
                    mBtnDelivery.setSelected(true);
                    switchFragment(2);
                }
                break;

        }
    }

    /**
     * 切换不同的fragment
     *
     * @param postion 切换的位置
     */
    public void switchFragment(int postion) {
        mPosition = postion;
        int cur1 = (postion + 1) % 3;
        int cur2 = (postion + 2) % 3;
        getSupportFragmentManager().beginTransaction().show(mFragmentList.get(postion))
                .hide(mFragmentList.get(cur1)).hide(mFragmentList.get(cur2))
                .commit();
        mFragmentList.get(postion).onResume();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }


    private void registMassage(){
        //注册广播接收到消息便自动刷新界面
        IntentFilter filter1 = new IntentFilter("action_jpush_check");
        if(receivePushMassage == null){
            receivePushMassage = new receivePushMassage();
            registerReceiver(receivePushMassage, filter1);
        }
    }

    class receivePushMassage extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            if (extras == null)
                return;
            String title = extras.getString("title");
            String json = extras.getString("type");
            mFragmentList.get(mPosition).onResume();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receivePushMassage);
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }
}
